package cn.iocoder.yudao.module.vos.controller.admin.pushlistconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vos.controller.admin.pushlistconfig.vo.*;
import cn.iocoder.yudao.module.vos.dal.dataobject.pushlistconfig.PushListConfigDO;
import cn.iocoder.yudao.module.vos.service.pushlistconfig.PushListConfigService;

@Tag(name = "管理后台 - 名单推送配置")
@RestController
@RequestMapping("/vos/push-list-config")
@Validated
public class PushListConfigController {

    @Resource
    private PushListConfigService pushListConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建名单推送配置")
    @PreAuthorize("@ss.hasPermission('vos:push-list-config:create')")
    public CommonResult<Long> createPushListConfig(@Valid @RequestBody PushListConfigSaveReqVO createReqVO) {
        return success(pushListConfigService.createPushListConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新名单推送配置")
    @PreAuthorize("@ss.hasPermission('vos:push-list-config:update')")
    public CommonResult<Boolean> updatePushListConfig(@Valid @RequestBody PushListConfigSaveReqVO updateReqVO) {
        pushListConfigService.updatePushListConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除名单推送配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vos:push-list-config:delete')")
    public CommonResult<Boolean> deletePushListConfig(@RequestParam("id") Long id) {
        pushListConfigService.deletePushListConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除名单推送配置")
                @PreAuthorize("@ss.hasPermission('vos:push-list-config:delete')")
    public CommonResult<Boolean> deletePushListConfigList(@RequestParam("ids") List<Long> ids) {
        pushListConfigService.deletePushListConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得名单推送配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vos:push-list-config:query')")
    public CommonResult<PushListConfigRespVO> getPushListConfig(@RequestParam("id") Long id) {
        PushListConfigDO pushListConfig = pushListConfigService.getPushListConfig(id);
        return success(BeanUtils.toBean(pushListConfig, PushListConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得名单推送配置分页")
    @PreAuthorize("@ss.hasPermission('vos:push-list-config:query')")
    public CommonResult<PageResult<PushListConfigRespVO>> getPushListConfigPage(@Valid PushListConfigPageReqVO pageReqVO) {
        PageResult<PushListConfigDO> pageResult = pushListConfigService.getPushListConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PushListConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出名单推送配置 Excel")
    @PreAuthorize("@ss.hasPermission('vos:push-list-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPushListConfigExcel(@Valid PushListConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PushListConfigDO> list = pushListConfigService.getPushListConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "名单推送配置.xls", "数据", PushListConfigRespVO.class,
                        BeanUtils.toBean(list, PushListConfigRespVO.class));
    }

}